package servlet.home;

import com.google.gson.Gson;
import data.Select;
import data.dataStruct.DataStruct;
import servlet.Control;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;

@WebServlet(name = "SelectProfileMsg",value = "/home/selectProfileMsg")
public class SelectProfileMsg extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tidJson=request.getParameter("tid");

        List<String> tid=new Gson().fromJson(tidJson,List.class);
        int page=Integer.parseInt(request.getParameter("page"));

        List<Map<String,Object>> profiles=new ArrayList<>();

        for(int i=(page-1)*8;i<tid.size()&&i<page*8;i++){
            //翻译员的详细信息
            Map<String,Object> map= Select.profileDetail(Integer.parseInt(tid.get(i)));
            //移除一些信息
            map.remove("pwd");
            map.remove("priority");
            map.remove("visit");

            //加入城市和翻译语言的查询
            List<DataStruct> city=Select.profileCity(Integer.parseInt(tid.get(i)));
            List<DataStruct> language=Select.profileLanguage(Integer.parseInt(tid.get(i)));
            map.put("city",city);
            map.put("language",language);

            profiles.add(map);
        }

        Map<String,Object> map=new HashMap<>();
        map.put("number",tid.size());
        map.put("data",profiles);

        String json=new Gson().toJson(map);

        Control.sendJson(response,json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
